package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /*

    员工登录
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){

        //1.密码进行md5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());


        //2.根据页面提交的用户名查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        // 调用业务层的条件查询功能
        Employee emp = employeeService.getOne(queryWrapper);

        //3.判断查询结果
        if(emp == null){
            return R.error("用户名错误");

        }
        //4.密码比对
        if(!emp.getPassword().equals(password)){
            return R.error("密码错误");
        }
        //5.查看员工状态
        if(emp.getStatus() == 0){
            return R.error("账号禁用");
        }
        //6登录成功，将id存入session
        request.getSession().setAttribute("employee",emp.getId());

        return R.success(emp);





    }

    //员工退出
    @PostMapping("/logout")
    public R<String>logout(HttpServletRequest request){
        //清理session中保存的当前员工id
        request.getSession().removeAttribute("employee");

        return R.success("退出成功");
    }
    //新增员工
    @PostMapping
    public R<String>save(HttpServletRequest request,@RequestBody Employee employee){
        //设置初始密码
        String password = "123456";
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        employee.setPassword(password);

        //employee.setCreateTime(LocalDateTime.now());
       // employee.setUpdateTime(LocalDateTime.now());
       // Long empid= (Long)request.getSession().getAttribute("employee");

        //employee.setCreateUser(empid);
        //employee.setUpdateUser(empid);

        employeeService.save(employee);
        return R.success("添加员工成功");
    }

    //员工信息分页查询
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize,String name){
        //log.info("p: {}   ps : {},  name:{}",page,pageSize,name);

        //构造分页构造器
        Page<Employee> pageInfo = new Page<>(page,pageSize);

        //构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        //执行查询
        employeeService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }
    //根据id修改信息
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
       /// log.info("{}",employee);
       // employee.setUpdateTime(LocalDateTime.now());
        //employee.setUpdateUser((Long)request.getSession().getAttribute("employee"));

        employeeService.updateById(employee);
        return R.success("修改成功");

    }
    //根据ID查询员工消息
    @GetMapping("/{id}")
    public  R<Employee> getById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }



}
